package me.zhanshi123.lab4.service.impl;

import me.zhanshi123.lab4.dao.GoodDao;
import me.zhanshi123.lab4.entity.ApiResponse;
import me.zhanshi123.lab4.entity.CategoryStatistics;
import me.zhanshi123.lab4.entity.Good;
import me.zhanshi123.lab4.entity.GoodFilter;
import me.zhanshi123.lab4.entity.paged.impl.PagedGoods;
import me.zhanshi123.lab4.service.IGoodService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class GoodService implements IGoodService {
    @Autowired
    private GoodDao goodsDao;

    public String addGood(Good good) {
        ApiResponse<String> apiResponse = new ApiResponse<>();
        try {
            boolean suc = goodsDao.addGood(good.getName(), good.getType(), good.getOrigin(), good.getPrice(), good.getProduction(), good.getManufacturer()) == 1;
            if (suc) {
                return apiResponse.success("ok");
            } else {
                return apiResponse.fail("数据库交互错误");
            }
        } catch (Exception e) {
            return apiResponse.fail("没有这个分类");
        }
    }

    public String deleteGood(int id) {
        boolean suc = goodsDao.removeGood(id) == 1;
        ApiResponse<String> apiResponse = new ApiResponse<>();
        if (suc) {
            return apiResponse.success("ok");
        } else {
            return apiResponse.fail("数据库交互错误");
        }
    }

    public String editGood(Good good) {
        goodsDao.updateGood(good.getId(), good.getName(), good.getType(), good.getOrigin(), good.getPrice(), good.getProduction(), good.getManufacturer());
        ApiResponse<String> apiResponse = new ApiResponse<>();
        return apiResponse.success("ok");
    }

    public String showGoods(GoodFilter pagedParameter) {
        if (pagedParameter.getFilter().getName().equals("")) {
            pagedParameter.getFilter().setName(null);
        }
        if (pagedParameter.getFilter().getOrigin().equals("")) {
            pagedParameter.getFilter().setOrigin(null);
        }
        if (pagedParameter.getFilter().getManufacturer().equals("")) {
            pagedParameter.getFilter().setManufacturer(null);
        }
        if (pagedParameter.getFilter().getType().equals("")) {
            pagedParameter.getFilter().setType(null);
        }
        ApiResponse<PagedGoods> pagedGoodsApiResponse = new ApiResponse<>();
        List<Good> goodList = goodsDao.getGoodsBy(pagedParameter.getStart(), pagedParameter.getSpan(), pagedParameter.getFilter().getId(),
                pagedParameter.getFilter().getName(), pagedParameter.getFilter().getType(), pagedParameter.getFilter().getOrigin(),
                pagedParameter.getFilter().getManufacturer());
        int count = goodsDao.getGoodsCount(pagedParameter.getFilter().getId(),
                pagedParameter.getFilter().getName(), pagedParameter.getFilter().getType(), pagedParameter.getFilter().getOrigin(),
                pagedParameter.getFilter().getManufacturer());
        return pagedGoodsApiResponse.success(new PagedGoods(goodList, pagedParameter.getStart(), count));
    }


    public String getStatistics() {
        ApiResponse<Map<String, Long>> apiResponse = new ApiResponse<>();
        List<CategoryStatistics> categoryStatistics = goodsDao.getCategoryStatistics();
        if (categoryStatistics == null) {
            return apiResponse.fail("数据库访问失败");
        }
        Map<String, Long> result = new HashMap<>();
        categoryStatistics
                .forEach(t -> result.put(t.getName(), t.getCount()));
        return apiResponse.success(result);
    }
}
